﻿using System;
using System.Collections.Generic;
using System.Text;
using XDesigner.ORM;

namespace XDesigner.AppFramework.DOM
{

    public class AppConfig
    {
        private int _ID = 0;
        [ORMKeyField()]
        public virtual int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }

        private string _Name = null;

        public virtual string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public string this[string name]
        {
            get
            {
                foreach (AppConfigItem item in this.Items)
                {
                    if (string.Compare(item.Name, name, true) == 0)
                    {
                        return item.Value;
                    }
                }
                return null;
            }
            set
            {
                foreach (AppConfigItem item in this.Items)
                {
                    if (string.Compare(item.Name, name, true) == 0)
                    {
                        item.Value = value;
                        return;
                    }
                }
                AppConfigItem newItem = new AppConfigItem();
                newItem.OwnerConfig = this;
                newItem.Name = name;
                newItem.Value = value;
                this.Items.Add(newItem);
            }
        }

        private List<AppConfigItem> _Items = null;
        [ORMRelation( ORMRelationStyle.OneToMany )]
        [ORMDelay()]
        public virtual List<AppConfigItem> Items
        {
            get { return _Items; }
            set { _Items = value; }
        }
    }

    [ORMType("Table_Config")]
    public class AppConfigItem
    {
        private int _RecordID = 0;
        [ORMKeyField()]
        public virtual int RecordID
        {
            get { return _RecordID; }
            set { _RecordID = value; }
        }

        private AppConfig _OwnerConfig = null;
        [ORMRelation(  ORMRelationStyle.OneToOne )]
        [ORMField("ConfigID")]
        public virtual AppConfig OwnerConfig
        {
            get { return _OwnerConfig; }
            set { _OwnerConfig = value; }
        }
         

        private string _Name = null;

        public virtual string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        private string _Value = null;

        public virtual string Value
        {
            get { return _Value; }
            set { _Value = value; }
        }

        private string _Description = null;

        public virtual string Description
        {
            get { return _Description; }
            set { _Description = value; }
        }
    }

}
